﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace StockPoint.Util
{
    public class ConvertUtil
    {
        public static XmlDocument StringToXmlDoc(string sData)
        {
            XmlDocument cXml = new XmlDocument();
            cXml.LoadXml(sData);

            return cXml;
        }

        public static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        public static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }

        public static Object DeserializeObject(String pXmlizedString)
        {
            //XmlSerializer xs = new XmlSerializer(typeof(Process));
            //MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
            //XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            //return xs.Deserialize(memoryStream);
            return null;
        }

        public static string ClassToXmlString(object cObject)
        {
            XmlSerializer xmls = new XmlSerializer(cObject.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.NewLineChars = Environment.NewLine;
                settings.ConformanceLevel = ConformanceLevel.Document;

                using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
                {
                    xmls.Serialize(writer, cObject);
                }

                string xml = Encoding.UTF8.GetString(ms.ToArray());
                xml = xml.Replace(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
                xml = xml.Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "").Trim();

                if (xml.Substring(0, 1) != "<")
                    xml = xml.Substring(1);

                if (xml.Substring(0, 1) != "<")
                    xml = xml.Substring(1);

                return xml;
            }
        }

        public static XmlDocument RemoveXMLEmptyNodes(XmlDocument myXmldocument) {
            XmlNodeList emptyXmlNodeList = myXmldocument.SelectNodes("//*[not(descendant::text()[normalize-space()])]");
            foreach (XmlNode emptyXmlNode in emptyXmlNodeList)
            {
                emptyXmlNode.ParentNode.RemoveChild(emptyXmlNode);
            }
            return myXmldocument;
        }

        public static string ClassToXml(object cObject)
        {
            XmlSerializer xmls = new XmlSerializer(cObject.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.NewLineChars = Environment.NewLine;
                settings.ConformanceLevel = ConformanceLevel.Document;

                using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
                {
                    xmls.Serialize(writer, cObject);
                }

                string xml = Encoding.UTF8.GetString(ms.ToArray());
                if (xml.Substring(0, 1) != "<")
                    xml = xml.Substring(1);

                XmlDocument cXml = new XmlDocument();
                cXml.LoadXml(xml);

                XmlDocument cXmlRemoveEmpty = RemoveXMLEmptyNodes(cXml);

                return cXml.OuterXml;
            }
        }

        public static XmlDocument ClassToXmlDoc(object cObject)
        {
            XmlSerializer xmls = new XmlSerializer(cObject.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.NewLineChars = Environment.NewLine;
                settings.ConformanceLevel = ConformanceLevel.Document;

                using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
                {
                    xmls.Serialize(writer, cObject);
                }

                string xml = Encoding.UTF8.GetString(ms.ToArray());
                xml = xml.Replace(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
                xml = xml.Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "").Trim();

                XmlDocument cXml = new XmlDocument();
                cXml.LoadXml(xml);

                return cXml;
            }
        }

        public static T XmlStringToClass<T>(string str) where T : class
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringReader reader = new StringReader(str);

            T c = serializer.Deserialize(reader) as T;

            return c;
        }

        public static string IntegerToStringNoZero(int iValue)
        {
            return iValue == 0 ? string.Empty : iValue.ToString();
        }

        public static int StringToIntegerNoEmpty(string sValue)
        {
            int iValue = 0;
            if (int.TryParse(sValue, out iValue) == true)
                return iValue;
            else
                return 0;
        }

        public static string StringToDateTimeString(string sDate, string sHour)
        {
            return sDate + " " + sHour.PadLeft(2, '0') + ":00:00";
        }

    }
}
